// basicnpc.txt
// A very simple, naive text. Creature attacks anything it hates nearby.
// Once it has won, it returns to its home.
// Memory Cells:
//   Cell 0 - How creature moves.
//     0 - If 0, wander randomly. 
//     1 - Stands still until a target appears.
//     2 - Completely immobile, even if target appears.
//   Cell 1,2 - Stuff done flag. If both 0, nothing. Otherwise when this 
//     is killed, set to 1. (Example: If cell 1 is 3 and cell 2 is 5, when
//     creature is killed, sets SDF(3,5) to 1.)
//   Cell 3 - Dialogue node to start with if talked to. if left at 0, this
//     character doesn't talk.

begincreaturescript;

variables;

short i, moves, target;
short haunt, haunt_x, haunt_y;
int j,k;

body;

beginstate INIT_STATE;
	if (get_memory_cell(0) == 2)
		set_mobility(ME,0);

 set_script_mode(3);
 haunt = 0;

	break;

beginstate DEAD_STATE;
	// Set the appropriate stuff done flag for this character being dead
	if ((get_memory_cell(1) != 0) || (get_memory_cell(2) != 0))
		set_flag(get_memory_cell(1),get_memory_cell(2),1);

place_monster(my_loc_x(),my_loc_y(),239,0);
erase_char(ME);

break;

beginstate START_STATE; 

moves = moves + 1;
change_char_health(ME,get_max_health(ME));

if (get_sdf(5,0) < 6) {

if (haunt == 0)
 haunt = random_group_member(1);

if ((haunt > 0) && (char_ok(haunt) == 0))
 haunt = random_group_member(1);

if (haunt > 0)
 approach_char(ME,haunt,3);

if (((dist_to_char(haunt) <= 2) && (haunt > 0)) || ((haunt > 0) && (moves > 50))) {
 if (haunt <= 12)
  set_flag(5,(haunt - 6),1);
 else
  set_flag(5,(haunt - 12),1);
 haunt_x = char_loc_x(haunt);
 haunt_y = char_loc_y(haunt);
 erase_char(haunt);
 place_monster(haunt_x,haunt_y,238,0);
 erase_char(ME);
 play_sound(163);
 end();
}

if ((get_sdf(5,0) == 2) && (moves == 4))
 message_dialog("_Must find a higher life form..._","");
if ((get_sdf(5,0) == 3) && (moves == 4))
 message_dialog("_These creatures are strong..._","");
if ((get_sdf(5,0) == 4) && (moves == 4))
 message_dialog("_Need an even better specimen..._","");
if ((get_sdf(5,0) == 5) && (moves == 4))
 message_dialog("_These bodies are too weak!_","");

}

if (get_sdf(5,0) >= 6) {

set_attitude(ME,4);

if ((get_sdf(5,6) == 0) && (moves >= 6)) {
 set_flag(5,6,1);
 moves = 0;
 i = 7;
 while (i <= 16) {
  erase_char(i);
  i = i +1;
 }
 j = 0;
 k = 0;
 set_character_pose(ME,1);
 force_instant_terrain_redraw();
 play_sound(123);
reset_dialog();
 add_dialog_str(0,"_My specimens! You're superior to them! But how? I will become strong enough to break free of this place! I must have you! Come to me. Give me your life!_",0);
 add_dialog_str(1,"Suddenly, the spirit changes color. You feel your life being slowly drained from you. With each passing moment, you grow more and more weary. You must find a way to defeat this creature.",0);
 add_dialog_str(2,"You realize that you are not strong enough to destroy it. Brute force cannot win. You must find another solution. Think! Think! Before it's too late...",0);
 add_dialog_choice(0,"OK.");
 run_dialog(1);
}

if ((j >= 5) && (get_sdf(5,7) == 0) && (get_sdf(5,9) == 0)) {
 set_flag(5,9,1);
 message_dialog("You start to feel the drain of the Perfect Spirit tearing at you. The sense is immense anger. Anger at many things. In particular you sense the anger at a device, an alien device.","That must be it! Somehow you have to find and disable it. There must be a way, but you have to get to it.");
}

if ((j >= 10) && (get_sdf(5,9) < 2)) {
 set_flag(5,9,2);
 message_dialog("Every moment you feel yourself getting weaker. You notice the Perfect Spirit chasing you is growing brighter and brighter as it swallows your life force. Despite this, its hunger continues to grow.","The pull is getting stronger! You have to act quickly.");
}

if ((j >= 20) && (get_sdf(5,9) == 2)) {
 set_flag(5,9,3);
 message_dialog("The pull is constantly becoming stronger and pulling you ever further down. With every passing moment, the strain becomes more and more unbarable. You have to find a way to defeat this thing!","");
}

if ((j > 20) && (get_sdf(5,7) == 1)) {
 k = k + 1;
 if (k == 15)
  message_dialog("Things are getting pretty drastic. You rack your brains for a solution. You must destroy the beams, redirect the energy. Redirect it, somehow. Then you remember the mirrors!","The mirrors can direct the beams! You think you saw a few somewhere. If you could reach them, you might be able to use them. All you know is you have to act fast.");
} 

if ((j >= 40) && (get_sdf(5,9) == 3)) {
 set_flag(5,9,4);
 message_dialog("The spirit continues to suck away at all of your living energy at an increasing rate. You realize that soon your healing spells will not be able to do enough to stave off the constant pull.","You must get to the mirrors and redirect the energy beams and destroy the device soon before you completely run out of life force.");
}

if ((j >= 60) && (get_sdf(5,9) == 4)) {
 set_flag(5,9,5);
 message_dialog("The stress is becoming impossible to deal with. Time is running out. Your healing spells are just being eaten away by the unrelenting spirit. You don't think you can survive much longer!","");
}

if (get_sdf(5,6) == 1) {
 set_character_pose(ME,1);
 if (moves >= 5) {
  stop_moving(ME);
  approach_char(ME,random_group_member(0),3);
  if ((get_ran(1,0,4) == 0) && (can_see_char(1000) == 1))
   play_sound(123);
  moves = 0;
  i = 0;
  while (i < 4) {
   if (get_ran(1,0,9) >= 2)
    if (get_health(i) == 0)
     kill_char(i,2,1);
    change_char_health(i,(0 - get_ran(1,0,(j/4))));
   i = i + 1;
  }
  j = j + 1;
 }
}


}

	// Otherwise, just peacefully move around. Go back to start, if I'm too far
	// from where I started.
	if ((my_dist_from_start() >= 6) || ((my_dist_from_start() > 0) && (get_memory_cell(0) > 0))) {
		if (get_ran(1,1,100) < 40) 
			return_to_start(ME,1);
		}
		else if (get_memory_cell(0) == 0) {
			fidget(ME,25);
			}

	// if we're in combat and the above didn't give me anything to do, just
	// stop now. Otherwise, game will keep running script, and that eats up CPU time.
	if (am_i_doing_action() == FALSE)
		end_combat_turn();
break;

beginstate 3; // attacking
	if (target_ok() == FALSE)
		set_state(START_STATE);
	do_attack();
break;

beginstate TALKING_STATE;
	if (get_memory_cell(3) == 0) {
		print_str("Talking: It doesn't respond.");
		end();
		}
	begin_talk_mode(get_memory_cell(3));
break;